package fr.vvins.core.catalog.entity;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import fr.vvins.core.dao.EntityImpl;
import fr.vvins.core.user.entity.UserComment;
import fr.vvins.core.user.entity.UserLog;
import fr.vvins.core.user.entity.UserRating;

/**
 * Vigneron entity
 */

@Entity
@Table(schema="vvins",name="VV_VIGNERON")

public class Vigneron extends EntityImpl implements Serializable {

	private static final long serialVersionUID = 715901145205930063L;
	
	@Id
	@GeneratedValue
	@Column(name="ID")
	private Integer id;
	
	@Basic
	@Column(name="FIRST_NAME")
	private String firstName;

	@Basic
	@Column(name="LAST_NAME")
	private String lastName;
	
	@ManyToOne
	@JoinColumn(name="CONTENT_ID")
	private Content content;
	
	@Basic
	@Column(name="DESCRIPTION")
	private String description;
	
	@Basic
	@Column(name="SELL")
	private Long sell;

	@Basic
	@Column(name="FAVORITE")
	private Long favorite;
	
	@Basic
	@Column(name="LOG")
	private Long log ;

	@Basic
	@Column(name="SCORE")
	private Long score ;
	
	@OneToMany(mappedBy="vigneron")
	private Set<Vin> vins;
	
	@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
	@JoinTable(schema="vvins", name="VV_VIGNERON_TAG", joinColumns=@JoinColumn(name="VIGNERON_ID"), inverseJoinColumns=@JoinColumn(name="TAG_ID"))
	private Set<Tag> tags = new HashSet<Tag>(0);
	
	@OneToMany(mappedBy="vigneron")
	private Set<UserLog> logs = new HashSet<UserLog>(0);
	
	@OneToMany(mappedBy="product")
	private Set<UserRating> userRatings = new HashSet<UserRating>(0);
	
	@OneToMany(mappedBy="vigneron")
	private Set<VigneronRating> ratings = new HashSet<VigneronRating>(0);
	
	@OneToMany(mappedBy="vigneron")
	private Set<UserComment> userComments = new HashSet<UserComment>(0);
	
	@Transient
	private Long bestPrice;
	
	@Transient
	private Set<Color> colors;
	
	@Transient
	private Set<Domain> domains;
	
	public Vigneron() {
	}

	public Vigneron(String firstName, String lastName, String description,
			 Set<Vin> Vins) {
		this.firstName = firstName;
		this.lastName = lastName;
		this.description = description;
		this.vins = Vins;
	}

	@Override
	public Integer getId() {
		return this.id;
	}

	@Override
	public void setId(Integer id) {
		this.id = id;
	}

	public String getFirstName() {
		return this.firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return this.lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getDescription() {
		return this.description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Set<Vin> getVins() {
		return vins;
	}

	public void setVins(Set<Vin> vins) {
		this.vins = vins;
	}

	public Content getContent() {
		return content;
	}

	public void setContent(Content content) {
		this.content = content;
	}

	public Set<Tag> getTags() {
		return tags;
	}

	public void setTags(Set<Tag> tags) {
		this.tags = tags;
	}

	public Long getSell() {
		return sell;
	}

	public void setSell(Long sell) {
		this.sell = sell;
	}

	public Long getFavorite() {
		return favorite;
	}

	public void setFavorite(Long favorite) {
		this.favorite = favorite;
	}

	public Long getLog() {
		return log;
	}

	public void setLog(Long log) {
		this.log = log;
	}

	public Long getScore() {
		return score;
	}

	public void setScore(Long score) {
		this.score = score;
	}

	public Set<UserLog> getLogs() {
		return logs;
	}

	public void setLogs(Set<UserLog> logs) {
		this.logs = logs;
	}

	public Long getBestPrice() {
		if(bestPrice == null) {
			for(Vin v : getVins()){
				for(Sku s : v.getSkus()){
					if(bestPrice == null){
						bestPrice = s.getPrice();
					} else if(s.getPrice() < bestPrice){
						bestPrice = s.getPrice();
					}
				}
			}
		}
		return bestPrice;
	}

	public void setBestPrice(Long bestPrice) {
		this.bestPrice = bestPrice;
	}

	public Set<Color> getColors() {
		if(colors == null) {
			colors = new HashSet<Color>();
			for(Vin v : getVins()){
				Color c = v.getColor();
				if(!colors.contains(c)){
					colors.add(c);
				}
			}
		}
		return colors;
	}

	public void setColors(Set<Color> colors) {
		this.colors = colors;
	}

	public Set<VigneronRating> getRatings() {
		return ratings;
	}

	public void setRatings(Set<VigneronRating> ratings) {
		this.ratings = ratings;
	}

	public Set<Domain> getDomains() {
		if(domains == null) {
			domains = new HashSet<Domain>();
			for(Vin v : getVins()){
				Domain d = v.getDomain();
				if(!domains.contains(d)){
					domains.add(d);
				}
			}
		}
		return domains;
	}

	public void setDomains(Set<Domain> domains) {
		this.domains = domains;
	}

	public Set<UserRating> getUserRatings() {
		return userRatings;
	}

	public void setUserRatings(Set<UserRating> userRatings) {
		this.userRatings = userRatings;
	}

	public Set<UserComment> getUserComments() {
		return userComments;
	}

	public void setUserComments(Set<UserComment> userComments) {
		this.userComments = userComments;
	}

}
